<template>
  <div>
    <!--    面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/administration' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评论管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <!--      搜索添加-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
                  placeholder="搜索评论"
                  prefix-icon="el-icon-search"
                  v-model="keyword"
                  @blur="searchComments"
                  @clear="searchComments"
                  clearable>
          </el-input>
        </el-col>
      </el-row>

      <!--    新闻列表-->
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column
                type="index"
                width="50">
        </el-table-column>
        <el-table-column
                prop="content"
                label="评论内容">
        </el-table-column>
        <el-table-column
                prop="username"
                label="用户">
        </el-table-column>
        <el-table-column
                prop="title"
                label="所属新闻">
        </el-table-column>
        <el-table-column
                prop="topic"
                label="所属主题">
        </el-table-column>
        <el-table-column
                prop="date"
                label="评论时间">
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeComments(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--      分页-->
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[3, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="comments.length">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  import {mapState} from 'vuex'

  export default {
    name: "Comments",
    data() {
      return {
        keyword: '',
        tableData: [],
        currentPage: 1,
        pageSize: 5
      }
    },
    methods: {
      getList() {
        this.$store.dispatch('getComments').then(() => {
          this.tableData = this.$store.state.news
          this.handleCurrentChange(this.currentPage)
          if (this.tableData.length === 0 && this.currentPage > 0) {
            this.currentPage--
            this.handleCurrentChange(this.currentPage)
          }
        })
      },
      removeComments(row) {
        this.$store.dispatch('removeComments', row._id)
          .then(() => {
            this.$message.success('删除成功')
            this.getList()
          })
      },
      searchComments() {
        this.$store.dispatch('searchComments', {keyword: this.keyword.trim()}).then(() => {
          this.handleCurrentChange(1)
        })
      },
      handleSizeChange(size) {
        this.pageSize = size
        this.handleCurrentChange(this.currentPage)
      },  //切换条数
      handleCurrentChange(page) {
        this.currentPage = page
        this.tableData = this.$store.state.comments.filter((item, index) => index >= this.pageSize * (this.currentPage - 1) && index < this.pageSize * this.currentPage)
      },  //切换页数
    },
    created() {
      this.getList()
    },
    computed: {
      ...mapState(['comments'])
    }
  }
</script>

<style scoped>
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .el-table {
    margin-top: 20px;
  }
</style>